import axios from 'axios'
import ElementUI from 'element-ui';

const request = axios.create({
  baseURL: `http://localhost:8081/`,
  timeout: 50000,
})

// request 拦截器
request.interceptors.request.use(config => {
  return config
}, error => {
  console.log('error:' + error)
  return Promise.reject(error)
});

// response 拦截器
// 可以在接口响应后统一处理结果
request.interceptors.response.use(response => {
    let res = response.data;
    console.log(res)
    //当抛出异常的时候给出提示
    if (res.code !== 200) {
      ElementUI.Message({
        message: res.message,
        type: 'error'
      });
    }
    return res;
  }, error => {
    console.log('error:' + error)
    return Promise.reject(error)
  }
)

export default request
